/**
 * Copyright (C) 2013 The yydorid-framework project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.yangying.androidframework.core.adapter;


import java.util.Iterator;

import net.yangying.androidframework.core.model.Group;
import net.yangying.androidframework.core.model.YYDroid;
import android.content.Context;
import android.widget.BaseAdapter;

abstract class BaseGroupAdapter<T extends YYDroid> extends BaseAdapter {

    Group<T> group = null;

    public BaseGroupAdapter(Context context) {
    }

    @Override
    public int getCount() {
        return (group == null) ? 0 : group.size();
    }

    @Override
    public Object getItem(int position) {
        return group.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isEmpty() {
        return (group == null) ? true : group.isEmpty();
    }

    public void setGroup(Group<T> group) {
        this.group = group;
        notifyDataSetInvalidated();
    }
    
    public Group<T> getGroup()
    {
    	return this.group;
    }

    public void appendGroup(Group<T> group) {
        if(this.group!=null)
        {
            Iterator<T> it = group.iterator();
            while (it.hasNext()) {
                append(it.next());
            }
        }else
            setGroup(group);

        notifyDataSetInvalidated();
    }

    public void append(T o) {
        group.add(o);
//        notifyDataSetInvalidated();
    }
}
